package cm.just;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "RegServlet",value="/register")
public class RegServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //获取传递过来的参数
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        System.out.println(userName+ password);
        Connection connect = null;
        PreparedStatement preparedStatement = null;
        String sql = "insert into t_user(userName,password) value(?,?)";

        try {
            connect = JDBCUtil.getConnect();
            preparedStatement = connect.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,password);
            preparedStatement.executeUpdate();
            //给页面返回一个注册成的信息
            response.setContentType("text/html;charset=utf-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("注册成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            JDBCUtil.close(connect);
        }

    }
}
